import { Options, Vue } from 'vue-class-component'
import navigator from '@/components/common/navigator.vue'
import store from '@/store'
import router from '@/router'
import { ElMessage } from 'element-plus'

interface iChallengeCreate {
  challengeName: string
  startTime: Date
  endTime: Date
  stockCode: string
  startingFunds: number
  maxParticipantsNumber: number
  introduction: string
}

@Options({
  components: {
    navigator
  }
})

export default class createChallenge extends Vue {
  createChallengeParams: iChallengeCreate = {
    challengeName: '',
    stockCode: '',
    startingFunds: 0,
    maxParticipantsNumber: 0,
    introduction: '',
    startTime: new Date(),
    endTime: new Date()
  }

  create (): void {
    if (this.createChallengeParams.challengeName === '') {
      ElMessage.warning({
        message: '请输入挑战赛名称',
        type: 'warning'
      })
    } else if (this.createChallengeParams.maxParticipantsNumber === 0) {
      ElMessage.warning({
        message: '请输入参赛人数',
        type: 'warning'
      })
    } else if (this.createChallengeParams.stockCode === '') {
      ElMessage.warning({
        message: '请选择股票代码',
        type: 'warning'
      })
    } else if (this.createChallengeParams.startingFunds === 0) {
      ElMessage.warning({
        message: '请输入起始资金',
        type: 'warning'
      })
    } else if (this.createChallengeParams.introduction === '') {
      ElMessage.warning({
        message: '请输入挑战赛介绍内容',
        type: 'warning'
      })
    } else if (this.createChallengeParams.endTime <= this.createChallengeParams.startTime) {
      ElMessage.warning('时间输入错误')
    } else {
      store.dispatch('launchChallenge', this.createChallengeParams)
        .then(response => {
          console.log('connect success')
          const code = response.data.code
          if (code === 200) {
            router.push({
              path: '/challenge',
              query: { data: response.data.data }
            })
          }
        })
        .catch(() => {
          console.log('connect failed')
        })
    }
  }

  cancel (): void {
    router.push({
      path: '/challenge'
    })
  }
}
